<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>新增固件</title>
    <meta name="ctx" th:content="${#httpServletRequest.getContextPath()}"/>
    <link rel="stylesheet" th:href="@{/css/admin/base-v2.css}">
    <link rel="stylesheet" th:href="@{/lib/font-awesome/4.7.0/css/font-awesome.min.css}">
    <link rel="stylesheet" th:href="@{/css/admin/app.css}">
    
    <style>
        .file-desc {
            width: 100%;
            border: 1px solid #ededed;
            padding: 10px;
        }

        .block {
            margin-bottom: 20px;
        }

        .btn-disabled {
            pointer-events: none;
            filter: alpha(opacity=50); /*IE滤镜，透明度50%*/
            -moz-opacity: 0.5; /*Firefox私有，透明度50%*/
            opacity: 0.5; /*其他，透明度50%*/
        }
    </style>	
</head>

<body>
<div class="app-page">
    <div class="app-page-content">
        <div class="app-card">
            <div class="app-card-body">
                <div class="app-card-body-con">
                    <div class="page-container">
                        <form class="form">
							<input type="hidden" name="url" data-name="url">
							 <div class="form-item row">
		                        <div class="label">
		                            <label>上传固件</label>
		                        </div>
		                        <div class="body" style="min-width: 350px;">
		                            <div class="upload upload-file z-mab" data-input-name="url" data-limit="1"
		                                 th:attr="data-url=@{/firmware/upload/firmware}" data-fieldname="files"
		                                 data-upload-success="uploadSuccess">
		                                <div class="upload-list upload-list-text"></div>
		                                <div class="upload-tips">只能上传固件文件，格式 .zip</div>
		                                <div class="upload-error"></div>
		                                <div class="input-opacity">
		                                    <input type="file" class="input" accept=".zip" style="max-width: 90px;">
		                                    <a class="z-btn z-default"><i class="fa fa-upload"></i> 上传</a>
		                                </div>
		                            </div>
		                        </div>
		                    </div>
                            <div class="form-item row">
                                <div class="label">
                                    <label>固件名称：</label>
                                </div>
                                <div class="body"> <input type="text" name="upgradeName" data-name="upgradeName"></div>
                            </div>
                            <div class="form-item row">
                                <div class="label">
                                    <label>版本名称：</label>
                                </div>
                                <div class="body"> <input type="text" name="versionName" data-name="versionName"> </div>
                            </div>
                            <div class="form-item row">
                                <div class="label">
                                    <label>大小（M）：</label>
                                </div>
                                <div class="body"> <input type="text" class="size" name="size" data-name="size" disabled="disabled"> </div>
                            </div>
                            <div class="form-item row">
                                <div class="label">
                                    <label>机型：</label>
                                </div>
                                <div class="body">
                                	<div th:each="deviceModel: ${deviceModelList}" style="display:inline-block">
                                		 <input class="input" type="checkbox" name="modelId" th:value="${deviceModel.deviceModelId}"/>
                        				 <label th:text="${deviceModel.deviceModel}"></label>
                                	</div>
                                </div>
                            </div>
                            <div class="form-item row">
                                <div class="label"><label>版本说明：</label></div>
                                <div class="body">
                                    <textarea class="z-df file-desc" name="description" data-name="description" cols="30" rows="10" maxlength="100" placeholder="请填写说明信息（选填）"></textarea>

                                </div>
                            </div>
                            <div class="form-item row">
                                <div class="body">
                                    <a class="btn-sub z-btn z-assist btn-submit">保存</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/lib/jquery.min.js}"></script>
<script th:src="@{/lib/layer-v3.1.1/layer/layer.js}"></script>
<script th:src="@{/js/popup.js}"></script>
<script th:src="@{/js/form.js}"></script>
<script th:src="@{/js/admin/app.js}"></script>
<script th:src="@{/js/admin-upload.js}"></script>
<script>
	//获取context path
	var context = $("meta[name='ctx']").attr("content");
	$(function () {
        // 保存
        $("form").form({
            submitSelector: ".btn-submit",
            msg: function (msg) {
                popup.tipsTop(msg, 'warning');
            },
            submit: function () {

            	   var data = {};
                   $.each($(".form").serializeArray(), function (index, field) {
                       if (data[field.name]) {
                           data[field.name] += "," + field.value;
                       } else {
                           data[field.name] = field.value;
                       }
                   });
                    data.size =$('.size').val();
                   // 固件文件是否上传
                   if (!data.url) {
                       return popup.tipsTop('请上传固件', 'warning');
                   }
                   if (!data.upgradeName) {
                       return popup.tipsTop('请填写固件名称', 'warning');
                   }
                   if (!data.modelId) {
                       return popup.tipsTop('请选择机型', 'warning');
                   }

                    if(!$('.btn-submit').hasClass('btn-disabled')){
                        $('.btn-submit').addClass('btn-disabled');
                    }

                   //保存固件
                   App.ajax({
                       type: "POST",
                       url: context + "/firmware/saveFirmware",
                       data: data,
                       loading: true,
                       success: function (obj) {
                           var code = obj.code;
                           if (code == 0) {
                               popup.tipsTop('新增成功', 'success');

                               setTimeout(function () {
                                   //跳转到列表界面
                                   parentOpen(context + "/firmware/list");
                               }, 500);

                           } else {
                               popup.tipsTop(obj.msg, 'warning');
                           }
                       },
                       error: function (obj) {
                           popup.tipsTop(obj.msg, 'error');
                           $('.btn-submit').removeClass('btn-disabled');
                       }
                   })
            }
        })
    })
    
    function parentOpen(url) {
  		$(window.parent.window)[0].location.href = url;
    }
	
	function back() {
		 parentOpen(context + "/firmware/list");
	}
	
	// 上传成功回调
    function uploadSuccess(data) {
    	var fileData = data[0];
        // 赋值apk名称和版本号
        $('[name="upgradeName"]').val(fileData["name"]);
        $('[name="versionName"]').val(fileData["versionName"]);
        $('[name="versionCode"]').val(fileData["versionCode"]);
        $('[name="size"]').val(fileData["size"]);
        $('[name="url"]').val(fileData["fileURL"]);
        $('[name="md5"]').val(fileData["md5"]);
    }
</script>
<script>
</script>

</body>

</html>